package com.example.order.controller.api;

import com.example.common.util.constant.OrderConstants;
import com.example.common.util.core.controller.BaseController;
import com.example.common.util.core.resultbody.AjaxResult;
import com.example.common.util.enums.PaymentEnums;
import com.example.common.util.utils.StringUtils;
import com.example.order.service.PaymentInfoService;
import com.example.order.service.WeiXinPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author juzipi
 * @Date 2021/7/23 20:53
 * @Info
 */
@Api(tags = "微信支付")
@RestController
@RequestMapping("api/order/weixin")
public class WeiXinPayApiController extends BaseController {


    @Autowired
    private WeiXinPayService weiXinPayService;

    @Autowired
    private PaymentInfoService paymentInfoService;



    @ApiOperation("获取支付二维码")
    @GetMapping("get/payQRCode/{orderId}")
    public AjaxResult getPayQRCode(@PathVariable Long orderId){
        Map<String, Object> payQRCode = weiXinPayService.getPayQRCode(orderId);
        return AjaxResult.success(payQRCode);
    }


    @ApiOperation("查询支付状态")
    @GetMapping("get/payType/{orderId}")
    public AjaxResult getPaymentType(@PathVariable Long orderId){
        Map<String, String> resultMap = weiXinPayService.getPaymentTypeByOrderId(orderId);
        if (StringUtils.isNull(resultMap)){
            return AjaxResult.error("操作失败");
        }
        if ("SUCCESS".equals(resultMap.get(OrderConstants.TRADE_STATE))){
            String outTradeNo = resultMap.get(OrderConstants.OUT_TRADE_NO);
            Integer integer = paymentInfoService.updatePaymentType(outTradeNo, PaymentEnums.WEIXIN.getStatus(), resultMap);
            return toAjax(integer);
        }
        return AjaxResult.success("支付中");
    }



}
